package main

import (
	"encoding/json"
	"log"
	"time"

	"github.com/nsqio/go-nsq"
)

type Message struct {
	Name      string
	Content   string
	Timestamp string
}

func main() {
	//The only valid way to instantiate the Config
	config := nsq.NewConfig()
	//Creating the Producer using NSQD Address
	producer, err := nsq.NewProducer(":4150", config)
	if err != nil {
		log.Fatal(err)
	}
	defer producer.Stop()
	//Init topic name and message
	topic := "Topic_Example"
	msg := Message{
		Name:      "Message Name Example",
		Content:   "Message Content Example",
		Timestamp: time.Now().String(),
	}
	//Convert message as []byte
	payload, err := json.Marshal(msg)
	if err != nil {
		log.Println(err)
	}

	for i := 0; i < 24; i++ {
		//Publish the Message
		err = producer.Publish(topic, payload)
		if err != nil {
			log.Println(err)
		}

		log.Println("publish succeeded.")
		time.Sleep(time.Second)
	}
}
